//
//  BQProductTableViewCell.m
//  BeeQuick
//
//  Created by AaronWei on 2017/7/9.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQProductTableViewCell.h"
#import "BQClickShopingView.h"
#import "BQProductModel.h"

@interface BQProductTableViewCell ()

@property (weak, nonatomic) BQClickShopingView *clickShopingView;

@property (weak, nonatomic) IBOutlet UIImageView *imgImageView;

@property (weak, nonatomic) IBOutlet UIImageView *isXfImageView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UIImageView *isPmImageView;

@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@property (weak, nonatomic) IBOutlet UILabel *market_priceLabel;

@end

@implementation BQProductTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    return self;
    
}

- (void)setModel:(BQProductModel *)model {
    
    _model = model;
    
    _clickShopingView.count = model.count;
    
    [self.imgImageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    
    self.nameLabel.text = model.name;
    
    self.specificsLabel.text = model.specifics;
    
    self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f", model.price];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%.2f", model.market_price] attributes:attribtDic];
    
    self.market_priceLabel.attributedText = attribtStr;
    
    if (model.had_pm) {
        self.isPmImageView.hidden = NO;
    } else {
        self.isPmImageView.hidden = YES;
    }
    
    if (!model.is_xf) {
        self.isXfImageView.hidden = YES;
        [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.imgImageView.mas_trailing).offset(8);
            make.trailing.offset(-8);
            make.top.equalTo(self.imgImageView).offset(10);
        }];
    } else {
        self.isXfImageView.hidden = NO;
        [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.isXfImageView.mas_trailing).offset(4);
            make.trailing.offset(-8);
            make.top.equalTo(self.isXfImageView);
        }];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    BQClickShopingView *shopingView = [BQClickShopingView clickShopingView];
    
    _clickShopingView = shopingView;
    
    [self.contentView addSubview:shopingView];
    
    [shopingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self).offset(-8);
        make.bottom.equalTo(self.imgImageView.mas_bottom).offset(-8);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(27);
    }];
    
    [shopingView addTarget:self action:@selector(didClickShopingView:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)didClickShopingView:(BQClickShopingView *)clickShopingView {
    
    NSLog(@"...");
    
    _model.count = clickShopingView.count;
    
    if (clickShopingView.isClickIncreaseButton) {
        
        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
        
        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        
        basicAnimation.toValue = @(2 * M_PI);
        
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
